// pages/userlogin/userlogin.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    // wx.hideLoading({
    //   complete: (res) => {},
    // })
  },
  toempower() {
    wx.navigateTo({
      url: '../empower/empower',
    })
  },
  // 授权微信登录
  // 授权微信登录
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '用于完善用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        let userinfo = res.userInfo
        console.log(res.userInfo, 'resres');
        if (res.userInfo) {
          api.updatewxinfo({ nickName: userinfo.nickName, avatarUrl: userinfo.avatarUrl }, res => {
            console.log(res, 'resresresres');
            let getuserInfo = wx.getStorageSync('userInfo')
            getuserInfo.litPic = userinfo.avatarUrl
            getuserInfo.name = userinfo.nickName
            that.setData({ userInfo: getuserInfo })
            wx.setStorageSync('userInfo', getuserInfo)
            if (res.data.code == 0) {
              wx.showToast({
                title: '授权成功',
                icon: 'none',
                duration: 1000
              })
            }
          })
        } else {
          wx.navigateTo({
            url: '../empower/empower',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '拒绝授权将无法进入小程序，请重新点击授权',
          icon: 'none'
        })
        console.log(res, 'ressss');
      },

    })
  },
  bindGetUserInfo(e) {
    console.log(e.detail.userInfo)
    let userinfo = e.detail.userInfo
    if (e.detail.userInfo) {
      api.updatewxinfo({ nickName: userinfo.nickName, avatarUrl: userinfo.avatarUrl }, res => {
        console.log(res, 'resresresres');
        let getuserInfo = wx.getStorageSync('userInfo')
        getuserInfo.litPic = userinfo.avatarUrl
        getuserInfo.name = userinfo.nickName
        that.setData({ userInfo: getuserInfo })
        wx.setStorageSync('userInfo', getuserInfo)
        if (res.data.code == 0) {
          wx.showToast({
            title: '授权成功',
            icon: 'none',
            duration: 1000
          })

        }
      })
    } else {
      wx.navigateTo({
        url: '../empower/empower',
      })

    }
  },
  // 授权手机号
  getPhoneNumber(e) {
    console.log(e, 'eeeee');
    let { userInfo } = that.data
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      api.wxphone({ encryptedData: e.detail.encryptedData, iv: e.detail.iv }, res => {
        if (res.data.code == 0) {
          let { phoneNumber } = res.data.entity.data
          userInfo.phone = phoneNumber
          wx.setStorageSync('userInfo', userInfo)
          wx.navigateBack()
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      })
    } else {
      wx.navigateBack()
    }

  },

  signout() {
    // wx.navigateBack()
  },
  toback() {
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let userInfo = wx.getStorageSync('userInfo')
    that.setData({ userInfo })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})